package v1

import (
	"errors"
	"github.com/gin-gonic/gin"
	"github.com/gin-gonic/gin/binding"
	"strconv"
	"zimuzu_cvn_web_api/config"
	"zimuzu_cvn_web_api/model"
	"zimuzu_cvn_web_api/pkg/errno"
	"zimuzu_cvn_web_api/router/api"
	"zimuzu_cvn_web_api/router/api/app/middleware"
	"zimuzu_cvn_web_api/service"
)

type YtaAPI struct {
}

func (a YtaAPI) Name() string {
	return "yta API"
}

func (a YtaAPI) Bind() {
	ytaAPI := config.C.Router.Group("/v1/app/yta", middleware.TrackLog(), middleware.AuthMiddleware(), middleware.IPRateLimiter())
	{
		ytaAPI.GET("check", checkYta)
		ytaAPI.GET("bind", bindYta)
		ytaAPI.GET("bind/update", reBindYta)
		ytaAPI.GET("coin/price", coinPrice)
		ytaAPI.GET("recharge", recharge)
		ytaAPI.GET("recharge/log", rechargeLog)
	}
}

//校验是否首次访问
func checkYta(c *gin.Context) {

	uid, _ := service.GetUserIdByRequest(c)
	res, err := model.CheckUserYtaAddress(uid, "")
	if err != nil {
		api.SendResponse(c, errors.New("校验首次访问失败"), nil)
		return
	}

	var response struct {
		Status  int    `json:"status"`
		Active  int    `json:"active"`
		Address string `json:"address"`
	}

	if res.Address == "" {
		response.Status = 0
		response.Address = ""
		response.Active = 0
	} else {
		response.Status = 1
		response.Address = res.Address
		response.Active = res.Status
	}

	errno.OK.Info = "获取成功"
	api.SendResponse(c, errno.OK, response)
	return
}

//首次绑定
func bindYta(c *gin.Context) {

	uid, _ := service.GetUserIdByRequest(c)
	err := model.AddYtaAddress(uid, c.Query("address"))
	if err != nil {
		api.SendResponse(c, errors.New("绑定YTA地址失败"), nil)
		return
	}
	errno.OK.Info = "绑定YTA地址成功"
	api.SendResponse(c, errno.OK, nil)
	return
}

//换绑YTA地址
type reBindYtaParams struct {
	U       string `form:"u" json:"u" `
	Address string `json:"address" form:"address" `
}

func reBindYta(c *gin.Context) {

	var params reBindYtaParams
	err := c.ShouldBindWith(&params, binding.Query)
	if err != nil {
		api.SendResponse(c, errno.ParamsError, nil)
		return
	}
	uid, _ := service.GetUserIdByRequest(c)
	ok := service.IsPayPasswordChecked(uid, params.U)
	if !ok {
		api.SendResponse(c, errno.SignError, nil)
		return
	}

	err = model.UpdateYtaAddress(uid, params.Address)
	if err != nil {
		api.SendResponse(c, errors.New("换绑YTA地址失败"), nil)
		return
	}
	errno.OK.Info = "换绑YTA地址成功"
	api.SendResponse(c, errno.OK, nil)
	return
}

//recharge
type rechargeParams struct {
	U   string  `form:"u" json:"u" binding:"required,excludes= "`
	Cvn float64 `json:"cvn" form:"cvn"`
}

//兑换YTA
func recharge(c *gin.Context) {
	var params rechargeParams
	err := c.ShouldBindWith(&params, binding.Query)
	if err != nil {
		api.SendResponse(c, errno.ParamsError, nil)
		return
	}
	uid, _ := service.GetUserIdByRequest(c)
	ok := service.IsPayPasswordChecked(uid, params.U)
	if !ok {
		api.SendResponse(c, errno.SignError, nil)
		return
	}
	err = service.RechargeYtaUseCVNT(uid, params.Cvn)
	if err != nil {
		api.SendResponse(c, err, nil)
		return
	}
	errno.OK.Info = "兑换成功"
	api.SendResponse(c, errno.OK, nil)
	return
}

//兑换记录
func rechargeLog(c *gin.Context) {
	var params model.PageParams
	params = model.NewDefaultPageParams()
	err := c.ShouldBindWith(&params, binding.Query)
	if err != nil {
		api.SendResponse(c, errno.ParamsError, nil)
		return
	}
	uid, _ := service.GetUserIdByRequest(c)
	res, total, err := model.GetYtaRechargeLog(uid, params.Page, params.Per, 0)
	if err != nil {
		api.SendResponse(c, errors.New("获取兑换记录失败"), nil)
		return
	}
	errno.OK.Info = strconv.Itoa(total)
	api.SendResponse(c, errno.OK, res)
	return
}

//今日币价
func coinPrice(c *gin.Context) {
	uid, _ := service.GetUserIdByRequest(c)
	res, err := service.GetCvntYtaPrice(uid)
	if err != nil {
		api.SendResponse(c, errors.New("兑换失败"), nil)
		return
	}
	errno.OK.Info = "获取成功"
	api.SendResponse(c, errno.OK, res)
	return
}
